<?php
require_once 'Mobitv/Admin/Action.php';

class StatisticAction extends Mobitv_Admin_Action
{
    public function execute()
    {
        //$this->checkPermission();
        $this->updateLoginStatus();
        $userId = $_SESSION['userid'];
        $userId = 0;
        $reportDao = Vega_Dao::factory('Mobitv_Admin_Report');

        $type = isset($_GET['t']) ? $_GET['t'] : 'wc'; // watch count

        switch($type)
        {
            case 'wc':
            default:
                //$data = $reportDao->getWatchCountList($userId);
        }

        $topList = $reportDao->getTopList($cpId);

        $mostWatchedVideoList = $mostDownloadedVideoList = $mostWatchedUserList = $mostDownloadedUserList = array();
        foreach($topList AS $item)
        {
            $mostWatchedVideos      = (array) json_decode($item['most_watched_video']);
            $mostDownloadedVideos   = (array) json_decode($item['most_downloaded_video']);
            $mostWatchedUsers       = (array) json_decode($item['most_watched_user']);
            $mostDownloadedUsers    = (array) json_decode($item['most_downloaded_user']);

            $mostWatchedVideoList       = int_array_merge($mostWatchedVideoList, $mostWatchedVideos);
            $mostDownloadedVideoList    = int_array_merge($mostDownloadedVideoList, $mostDownloadedVideos);
            $mostWatchedUserList        = int_array_merge($mostWatchedUserList, $mostWatchedUsers);
            $mostDownloadedUserList     = int_array_merge($mostDownloadedUserList, $mostDownloadedUsers);
        }
        
        // reorder
        arsort($mostWatchedVideoList);
        arsort($mostDownloadedVideoList);
        arsort($mostWatchedUserList);
        arsort($mostDownloadedUserList);
        //var_dump($mostWatchedVideoList); die;

        $this->_view->assign('mostWatchedVideoList',    $mostWatchedVideoList);
        $this->_view->assign('mostDownloadedVideoList', $mostDownloadedVideoList);
        $this->_view->assign('mostWatchedUserList',     $mostWatchedUserList);
        $this->_view->assign('mostDownloadedUserList',  $mostDownloadedUserList);
    }
}

function int_array_merge($arr1, $arr2)
{
    if(!empty($arr2))
    {
        foreach($arr2 AS $key => $item)
        {
            if(isset($arr1[$key]))
            {
                $arr1[$key] += intval($item);
            }
        }

        return (array)$arr1 + (array)$arr2;
    }

    return $arr1;
}

